package com.exam.serviceimpl;

import com.exam.entity.DataDictionary;
import com.exam.mapper.DataDictionaryRepository;
import com.exam.service.DataDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DataDictionaryServiceImpl implements DataDictionaryService {

    // 注入
    @Autowired
    private DataDictionaryRepository dataDictionaryRepository;

    // 创建
    @Override
    public DataDictionary createDataDictionary(DataDictionary dataDictionary) {
        return dataDictionaryRepository.save(dataDictionary);
    }

    // 更新
    @Override
    public DataDictionary updateDataDictionary(Long id, DataDictionary dataDictionary) {
        DataDictionary existingDataDictionary = dataDictionaryRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("DataDictionary not found"));
        existingDataDictionary.setCode(dataDictionary.getCode());
        existingDataDictionary.setValue(dataDictionary.getValue());
        existingDataDictionary.setDescription(dataDictionary.getDescription());
        return dataDictionaryRepository.save(existingDataDictionary);
    }

    // 删除
    @Override
    public void deleteDataDictionary(Long id) {
        dataDictionaryRepository.deleteById(id);
    }

    // 查询
    @Override
    public DataDictionary getDataDictionary(Long id) {
        return dataDictionaryRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("DataDictionary not found"));
    }

    // 查询所有
    @Override
    public List<DataDictionary> getAllDataDictionaries() {
        return dataDictionaryRepository.findAll();
    }

}